package com.ledim.activity;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.ledim.activity.base.LeDimBaseActivity;
import com.ledim.bean.base.BaseResultInfo;
import com.ledim.utils.LedimBaseUtils;
import com.letv.android.young.client.R;
import com.ledim.service.ApiService;
import com.ledim.service.LedimApiServiceFactory;
import com.ledim.service.LedimHttpObserver;
import com.ledim.utils.ToastUtil;
import com.ledim.widget.view.LoadingBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 找回密码页
 */
public class UserFindPasswordActivity extends LeDimBaseActivity {

    private TextView mTitle;
    private TextView mEmailTab;
    private TextView mSmsTab;
    private ImageView mBack;
    private View mSmsLayout;
    private View mEmailLayout;
    private View mTabLine;
    private View mTabLayout;
    private Button mSendSms;
    private Button mSendEmail;
    private EditText mEmail;
    private String mEmailStr;
    private int mMoveWidth;
    private int mStart;
    private TranslateAnimation mAnimation;
    private LoadingBar mLoading;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_user_find_password;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        mBack = (ImageView) findViewById(R.id.user_top_view_back);
        mTitle = (TextView) findViewById(R.id.user_top_view_title);
        mSmsTab = (TextView) findViewById(R.id.user_find_sms);
        mEmailTab = (TextView) findViewById(R.id.user_find_email);
        mEmailLayout = findViewById(R.id.user_find_email_layout);
        mSmsLayout = findViewById(R.id.user_find_sms_layout);
        mTabLine = findViewById(R.id.user_find_tab_line);
        mTabLayout = findViewById(R.id.tabs);
        mEmail = (EditText) findViewById(R.id.user_find_password);
        mSendSms = (Button) findViewById(R.id.user_password_sms_btn);
        mSendEmail = (Button) findViewById(R.id.user_password_email_btn);

        mBack.setOnClickListener(this);
        mSendSms.setOnClickListener(this);
        mSendEmail.setOnClickListener(this);
        mSmsTab.setOnClickListener(this);
        mEmailTab.setOnClickListener(this);

        mTitle.setText("找回密码");
        mTitle.setVisibility(View.VISIBLE);
        mSmsLayout.setVisibility(View.VISIBLE);
        mSendSms.setVisibility(View.VISIBLE);
        mEmailLayout.setVisibility(View.GONE);
        mSendEmail.setVisibility(View.GONE);

        ViewTreeObserver vto_one = mTabLayout.getViewTreeObserver();
        vto_one.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                int width = mTabLayout.getMeasuredWidth();
                mMoveWidth = width / 5;
                return true;
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.user_top_view_back:
                finish();
                break;

            case R.id.user_find_sms:
                mSmsLayout.setVisibility(View.VISIBLE);
                mSendSms.setVisibility(View.VISIBLE);
                mEmailLayout.setVisibility(View.GONE);
                mSendEmail.setVisibility(View.GONE);
                mSmsTab.setTextColor(Color.parseColor("#333333"));
                mEmailTab.setTextColor(Color.parseColor("#999999"));
                mAnimation = new TranslateAnimation(mStart, 0, 0, 0);
                mAnimation.setDuration(300);
                mAnimation.setFillEnabled(true);
                mAnimation.setFillAfter(true);
                mTabLine.startAnimation(mAnimation);
                mStart = 0;
                break;
            case R.id.user_find_email:
                mSmsLayout.setVisibility(View.GONE);
                mSendSms.setVisibility(View.GONE);
                mEmailLayout.setVisibility(View.VISIBLE);
                mSendEmail.setVisibility(View.VISIBLE);
                mSmsTab.setTextColor(Color.parseColor("#999999"));
                mEmailTab.setTextColor(Color.parseColor("#333333"));
                mAnimation = new TranslateAnimation(mStart, mMoveWidth * 2, 0, 0);
                mAnimation.setDuration(300);
                mAnimation.setFillEnabled(true);
                mAnimation.setFillAfter(true);
                mTabLine.startAnimation(mAnimation);
                mStart = mMoveWidth * 2;
                break;
            case R.id.user_password_sms_btn:
                Intent intent =
                        new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:1069032901305721"));
                intent.putExtra("sms_body", "");
                startActivity(intent);
                break;
            case R.id.user_password_email_btn:
                mEmailStr = mEmail.getText().toString().trim();
                if (mEmailStr == null || mEmailStr.length() == 0) {
                    ToastUtil.toast("请输入邮箱地址");
                    mEmail.requestFocus();
                } else if (!LedimBaseUtils.isEmail(mEmailStr)) {
                    ToastUtil.toast("邮箱格式不正确");
                    mEmail.requestFocus();
                } else {
                    //接口处理
                    findPassword(mEmailStr);
                }
                break;

        }
    }

    /**
     * 找回密码
     *
     * @param email
     */
    private void findPassword(String email) {
        if (mLoading == null) {
            mLoading = new LoadingBar(this);
        }
        mLoading.show();
        LedimApiServiceFactory.createService(ApiService.class)
                              .findPassword(email)
                              .observeOn(AndroidSchedulers.mainThread())
                              .subscribeOn(Schedulers.io())
                              .subscribe(new LedimHttpObserver<BaseResultInfo>() {
                                 @Override
                                 public void onCompleted() {
                                     super.onCompleted();
                                 }

                                 @Override
                                 public void onError(Throwable e) {
                                     super.onError(e);
                                     mLoading.dismiss();
                                     ToastUtil.toast(e.getMessage());
                                 }

                                 @Override
                                 public void onNext(BaseResultInfo data) {
                                     super.onNext(data);
                                     mLoading.dismiss();
                                     if (data != null) {
                                         if (data.success) {
                                             ToastUtil.toast("发送成功");
                                             finish();
                                         } else {
                                             //登录失败
                                             ToastUtil.toast(data.message);
                                         }
                                     }
                                 }
                             });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mLoading != null) {
            mLoading.dismiss();
        }
    }
}

